package gl.lu.l3tcraftsounds;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.ads.AdRequest;
import com.google.ads.AdView;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;

public class Chincheto77 extends Fragment {
	
	ReproducirSonido RS = new ReproducirSonido();

	public Chincheto77() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		Cursor fila = null;
		
		ArrayList<HashMap<String, String>> DatosAlex = new ArrayList<HashMap<String, String>>();
				
		BaseDeDatos b77 = new BaseDeDatos(getActivity().getApplicationContext());
		
		SQLiteDatabase db = b77.getReadableDatabase();
		
		if(db != null)
		{				
			fila = db.rawQuery("select * from Frases WHERE autor=1", null);
			fila.moveToFirst();
			db.close();
			b77.close();  
		}
		do{
		//	if(fila.getInt(3)!=0){
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("titulo", fila.getString(1));
				map.put("subtitulo",fila.getString(2));
				DatosAlex.add(map);
	//		}
		}while(fila.moveToNext());
		fila.close();
		
		final View v = LayoutInflater.from(getActivity()).inflate(R.layout.listview_chincheto, null);
		
		final ListView lv_chin = (ListView)v.findViewById(R.id.lv_Lista_Sonidos);

		SimpleAdapter lista_Alex = new SimpleAdapter(this.getActivity(), DatosAlex, R.layout.sonidochincheto_layout,
				new String[]{"titulo"}, new int[]{R.id.titulo}	);
		
		lv_chin.setAdapter(lista_Alex);
		lv_chin.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {	

				RS.playSonido(lv_chin, position,1 ,getActivity().getApplicationContext());
			}
		});
		return v;
	}
	
	public void onPause(){
        super.onPause();

        RS.getReproSonido().reset();
        RS.getReproSonido().stop();
    }

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		AdView adView = ( AdView)getView().findViewById(R.id.adView2);
	    adView.loadAd(new AdRequest());
	}
}
